function UiAssistant() {
}

UiAssistant.prototype.setup = function() {
	this.days = [$L("Sunday"), $L("Monday"), $L("Tuesday"), $L("Wednesday"), $L("Thursday"), $L("Friday"), $L("Saturday")];
	
	this.timeOpen = [];
	this.timeClose = [];
	
	this.businessNameAttributes = {
		hintText: $L("Business Name or Description"),
		multiline: false,
		enterSubmits: false,
		autoFocus: true,
		textCase: Mojo.Widget.steModeTitleCase,
		focusMode: Mojo.Widget.focusSelectMode
	}
	
	this.businessNameModel = {
		value: ""
	}
	
	this.controller.setupWidget("businessName", this.businessNameAttributes, this.businessNameModel);
	
	this.businessLocationAttributes = {
		hintText: $L("Business Location"),
		multiline: false,
		enterSubmits: false,
		textCase: Mojo.Widget.steModeTitleCase,
		focusMode: Mojo.Widget.focusSelectMode
	}
	
	this.businessLocationModel = {
		value: ""
	}
	
	this.controller.setupWidget("businessLocation", this.businessLocationAttributes, this.businessLocationModel);
	
	this.businessPhoneAttributes = {
		hintText: $L("Phone Number"),
		multiline: false,
		enterSubmits: false,
		modifierState: Mojo.Widget.numLock,
		focusMode: Mojo.Widget.focusSelectMode
	
	}
	
	this.businessPhoneModel = {
		value: ""
	}
	
	this.controller.setupWidget("businessPhone", this.businessPhoneAttributes, this.businessPhoneModel);
	
	this.generalAvailabilityAttributes = {
		labelPlacement: Mojo.Widget.labelPlacementRight,
		label: $L("Availability"),
		choices: [{
			label: $L("Custom Schedule"),
			value: 1
		}, {
			label: $L("Always Open"),
			value: 2
		}, {
			label: $L("By Appointment"),
			value: 3
		}]
	}
	
	this.generalAvailabilityModel = {
		value: 1,
		disabled: false
	}
	
	this.controller.setupWidget("generalAvailability", this.generalAvailabilityAttributes, this.generalAvailabilityModel);
	this.controller.listen("generalAvailability", Mojo.Event.propertyChanged, this.toggleCustomScheduleDrawer.bindAsEventListener(this));
	
	this.controller.setupWidget("customScheduleDrawer", {
		unstyled: true
	}, {
		open: true
	});
	
	for (var thisDayIndex = 1; thisDayIndex <= 7; thisDayIndex++) {
	
		this.timeOpen[thisDayIndex] = new Date();
		this.timeOpen[thisDayIndex].setFullYear(1984, 0, thisDayIndex);
		this.timeOpen[thisDayIndex].setHours(8, 0, 0, 0);
		
		this.timeClose[thisDayIndex] = new Date();
		this.timeClose[thisDayIndex].setFullYear(1984, 0, thisDayIndex);
		this.timeClose[thisDayIndex].setHours(17, 0, 0, 0);
		
		// Setup the open/closed toggle widget for each day
		this.controller.setupWidget("availableToggleDay" + thisDayIndex, {
			trueLabel: $L("Open"),
			trueValue: true,
			falseLabel: $L("Closed"),
			falseValue: false
		}, {
			value: true,
			disabled: false,
			dayNumber: thisDayIndex,
			dayName: this.days[thisDayIndex - 1]
		});
		
		// Setup the open/closed toggle listener
		this.controller.listen("availableToggleDay" + thisDayIndex, Mojo.Event.propertyChanged, this.toggleDayDrawer.bindAsEventListener(this));
		
		// Setup the drawer widget for each day
		this.controller.setupWidget("drawerDay" + thisDayIndex, {
			unstyled: true
		}, {
			open: true
		});
		
		// Setup the open-hour time picker widget for each day
		this.controller.setupWidget("timeOpenPickerDay" + thisDayIndex, {
			label: $L("Opens"),
			minuteInterval: 15
		}, {
			time: this.timeOpen[thisDayIndex],
			dayNumber: thisDayIndex
		});
		
		this.controller.listen("timeOpenPickerDay" + thisDayIndex, Mojo.Event.propertyChange, this.handleTimePickerChange.bindAsEventListener(this));
		
		// Setup the close-hour time picker widget for each day
		this.controller.setupWidget("timeClosePickerDay" + thisDayIndex, {
			label: $L("Closes"),
			minuteInterval: 15
		}, {
			time: this.timeClose[thisDayIndex],
			dayNumber: thisDayIndex
		});
		
		this.controller.listen("timeClosePickerDay" + thisDayIndex, Mojo.Event.propertyChange, this.handleTimePickerChange.bindAsEventListener(this));
		
		updateDayInfoText(this.controller.get("infoTextDay" + thisDayIndex), this.timeOpen[thisDayIndex], this.timeClose[thisDayIndex]);
	}
};

UiAssistant.prototype.toggleCustomScheduleDrawer = function(event) {
	var isOpen = "";
	
	switch (event.value.toString()) {
		case "1":
			isOpen = true;
			break;
		case "2":
			isOpen = false;
			break;
		case "3":
			isOpen = false;
			break;
	}
	this.controller.setWidgetModel("customScheduleDrawer", {
		open: isOpen
	});
};

UiAssistant.prototype.toggleDayDrawer = function(event) {
	var toggledDrawer = "drawerDay" + event.model.dayNumber;
	this.controller.setWidgetModel(toggledDrawer, {
		open: event.value
	});
};

UiAssistant.prototype.handleTimePickerChange = function(event) {
	var timeOpenObj = this.timeOpen[event.model.dayNumber];
	var timeCloseObj = this.timeClose[event.model.dayNumber];
	
	updateDayTimeModel(timeOpenObj, timeCloseObj);
	updateDayInfoText(this.controller.get("infoTextDay" + event.model.dayNumber), timeOpenObj, timeCloseObj);
}

UiAssistant.prototype.activate = function(event) {
};

UiAssistant.prototype.deactivate = function(event) {
};

UiAssistant.prototype.cleanup = function(event) {
	this.controller.stopListening("generalAvailability", Mojo.Event.propertyChanged, this.toggleCustomScheduleDrawer.bindAsEventListener(this));
	
	for (var thisDayIndex = 1; thisDayIndex <= 7; thisDayIndex++) {
		this.controller.stopListening("availableToggleDay" + thisDayIndex, Mojo.Event.propertyChanged, this.toggleDayDrawer.bindAsEventListener(this));
		this.controller.stopListening("timeOpenPickerDay" + thisDayIndex, Mojo.Event.propertyChange, this.handleTimePickerChange.bindAsEventListener(this));
		this.controller.stopListening("timeClosePickerDay" + thisDayIndex, Mojo.Event.propertyChange, this.handleTimePickerChange.bindAsEventListener(this));
	}
};

